<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/5/13
 * Time: 3:02 下午
 */

namespace app\index\controller;

use app\index\model\doCate;
use app\index\model\doGoods;
use app\index\model\doUser;
use think\Db;
use think\facade\Request;

class Warehouse extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    public function index(){

        $list = Db::name("warehouse wh")
                ->join("cate wc",'wh.cate_id=wc.id')
                ->where("wh.if_del",0)
                ->field("wh.*,wc.title as ctitle")
                ->paginate(10);
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return $this->fetch();
    }
    public function warecate(){
      $list = Db::name("warecate")->where("if_del",0)->paginate(10);

      $this->assign('list',$list);
      $this->assign('page',$list->render());
      return $this->fetch();
    }
    public function addcate(){
        if(Request::isPost()){
            $data = input("post.");
            $data['time'] = time();
            $data['status'] = 1;
            $data['if_del'] = 0;

            $res = Db::name("warecate")->insert($data);
            if($res){
                return json(['code'=>1,'msg'=>'增加成功']);
            }else{
                return json(['code'=>0,'msg'=>'网络错误']);
            }
        }
        return $this->fetch();
    }
    public function changecatestatus(){
        $id = input('id');
        $info = Db::name("warecate")->where('id',$id)->find();
        if($info['status']==1){
            $status = 0;
        }else{
            $status = 1;
        }
        $res = Db::name("warecate")->where('id',$id)->update(['status'=>$status]);
        if($res){
           return  json(['code'=>1,'msg'=>'变更成功']);
        }
        return  json(['code'=>0,'msg'=>'网路错误']);
    }
    public function changewarestatus(){
        $id = input('id');
        $info = Db::name("warehouse")->where('id',$id)->find();
        if($info['status']==1){
            $status = 0;
        }else{
            $status = 1;
        }
        $res = Db::name("warehouse")->where('id',$id)->update(['status'=>$status]);
        if($res){
            return  json(['code'=>1,'msg'=>'变更成功']);
        }
        return  json(['code'=>0,'msg'=>'网路错误']);
    }
    public function editcate(){
        if(Request::isPost()){
            $data = input("post.");
            $res = Db::name("warecate")->where('id',$data['id'])->update($data);
            if($res){
                return json(['code'=>1,'msg'=>'更新成功']);
            }
            return json(['code'=>0,'msg'=>'更新失败']);
        }
        $id = input('id');
        $data = Db::name("warecate")->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function delcate(){
        $id = input('id');
        $res = Db::name("warecate")->where('id',$id)->update(['if_del'=>1]);
        if($res){
            return  json(['code'=>1,'msg'=>'删除成功']);
        }
        return  json(['code'=>0,'msg'=>'网路错误']);
    }
    public function addWare(){
        if(Request::isPost()){
            $data = input("post.");
            $data['time'] = time();
            $data['if_del'] = 0;
            $data['status'] = 1;
//                dump($data);die;
            $res = Db::name("warehouse")->insert($data);
            if($res){
                return json(['code'=>1,'msg'=>'增加成功']);
            }else{
                return json(['code'=>0,'msg'=>'网络错误']);
            }
        }
        $cate = Db::name("cate")->where('status',1)->select();
        $this->assign("cate",$cate);
        return $this->fetch();
    }
    public function delware(){
        $id = input('id');
        $res = Db::name("warehouse")->where('id',$id)->update(['if_del'=>1]);
        if($res){
            return  json(['code'=>1,'msg'=>'删除成功']);
        }
        return  json(['code'=>0,'msg'=>'网路错误']);
    }
    public function editware(){
        if(Request::isPost()){
            $data = input("post.");
            $res = Db::name("warehouse")->where('id',$data['id'])->update($data);
            if($res){
                return json(['code'=>1,'msg'=>'更新成功']);
            }
            return json(['code'=>0,'msg'=>'更新失败']);
        }
        $id = input('id');
        $data = Db::name("warehouse")->where('id',$id)->find();
        $cate = Db::name("cate")->where("status",1)->select();
        $this->assign('data',$data);
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    public function waregoods(){
        $id = input("id");
        $goods_list = Db::name("ware_goods wg")
                    ->join("goods g",'wg.gid=g.id')
                    ->join("cate c",'g.cid=c.id')
                    ->where("wid",$id)
                    ->field('wg.*,g.*,c.title as ctitle')
                    ->paginate(10);
        $this->assign("page",$goods_list->render());
        $this->assign("goods",$goods_list);
        return $this->fetch();
    }
}